<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
//屏蔽错误
error_reporting(E_ALL^E_NOTICE);

use think\Db;

//助手函数--success
function success($msg = '操作成功',$url = ''){
  $data['status'] = 200;
  $data['msg'] = $msg;
  $data['url'] = $url;
  return json($data);
}

//助手函数--error
function error($msg = '操作失败', $url = ''){
  $data['status'] = 202;
  $data['msg'] = $msg;
  $data['url'] = $url;
  return json($data);
}
function get_ueditor($name,$value=''){
	$str=<<<HTML
	<textarea id='$name' name='$name'>$value</textarea>
	<script type="text/javascript">
    UE.getEditor('$name',{initialFrameWidth:1338,initialFrameHeight:400,})   
</script> 
HTML;
	return $str;
}
function table_exists($tablename='')
{
    //获取所有数据表名
    $tables = [];
    $data = Db::query('SHOW TABLES');
    foreach ($data as $value) {
        $tables[] = $value['Tables_in_'.config('database.database')];
    }
    //获取表前缀
    $dbPrefix = config('database.prefix');
    //当前的表名
    $tablename=$dbPrefix.$tablename;
    if(in_array($tablename,$tables)){
        return true; //存在
    }else {
        return false; //不存在
    }
}
/**
 * [field_exists 检测数据表字段是否存在]
 * @param  string $tablename [表名，不含前缀]
 * @param  string $field [字段名]
 * @return [bool]            [存在返回true，不存在返回false]
 */
function field_exists($tablename='', $field='')
{
    //获取表前缀
    $dbPrefix = config('database.prefix');
    //先判断数据表是否存在
    if(table_exists($tablename)){
        $fieldArray = Db::query("Describe `{$dbPrefix}{$tablename}` `{$field}` ;");
        if(is_array($fieldArray[0])){
            return true; //存在
        }else {
            return false; //不存在
        }
    }else {
        return false;  //不存在
    }
}

// 根据分类ID获取相应分类信息
function getCatInfoById($id=0, $field=''){
    if($field == ''){
        //获取单条数据
        return Db::name('category')->where('id',$id)->find();
    }else{
        //获取某个字段
        return Db::name('category')->where('id',$id)->value($field);
    }
}

// 根据分类ID获取相应模型信息
function getModInfoById($id=0, $field=''){
    //模型ID、
    $modelId = getCatInfoById($id, 'modelid');
    if($field == ''){
        //获取单条数据
        return Db::name('models')->where('id',$modelId)->find();
    }else{
        //获取某个字段
        return Db::name('models')->where('id',$modelId)->value($field);
    }
}

function timeToDate($time){
    return date("m-d",strtotime($time));
}

